package com.msi.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import org.apache.log4j.Logger;

import com.msi.Graph;
import com.msi.GraphGenerator;
import com.msi.visualization.GraphVisualization;

public class GenerateWindow extends JDialog {

    protected static final Logger log = Logger.getLogger(GenerateWindow.class);

    private JPanel panel;
    private Settings settings = Settings.getInstance();
    private JTextArea fillingValue;
    private JTextArea vertexValue;

    public GenerateWindow() {
        initUI();
    }

    public final void initUI() {
        panel = new JPanel();
        getContentPane().add(panel);

        panel.setLayout(null);
        panel.setToolTipText("A Panel container");

        addButtons(panel);
        addGenerateField();

        setTitle("Generator");
        setSize(350, 200);
        setLocationRelativeTo(null);
        // setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private void addGenerateField() {
        addFilling();
        addVertexCount();
    }

    private void addVertexCount() {
        JTextArea vertexText = new JTextArea("Liczba wierzchołków:");
        vertexText.setEditable(false);
        vertexText.setBounds(20, 30, 200, 15);
        vertexText.setToolTipText("Liczba ca�kowita");
        panel.add(vertexText);

        vertexValue = new JTextArea("" + settings.getVertexCount());
        vertexValue.setBounds(250, 30, 50, 15);
        vertexValue.setToolTipText("liczba ca�kowita");
        panel.add(vertexValue);
    }

    private void addFilling() {
        JTextArea fillingText = new JTextArea("Wypełnienie grafu:");
        fillingText.setEditable(false);
        fillingText.setBounds(20, 80, 200, 15);
        fillingText.setToolTipText("Liczba z zakresu 0 - 1");
        panel.add(fillingText);

        fillingValue = new JTextArea("" + settings.getFilling());
        fillingValue.setBounds(250, 80, 50, 15);
        fillingValue.setToolTipText("Liczba z zakresu 0 - 1");
        panel.add(fillingValue);
    }

    private void addButtons(JPanel panel) {
        JButton button = new JButton("Generate");
        button.setBounds(40, 120, 100, 30);
        button.setToolTipText("Generate");
        setListenerSet(button);
        panel.add(button);

        JButton button1 = new JButton("Cancel");
        button1.setBounds(180, 120, 100, 30);
        button1.setToolTipText("Cancel");
        panel.add(button1);
    }

    private void setListenerSet(JButton button) {
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setFillingValue();
                setVertex();
                Graph g = GraphGenerator.generateGraph(Settings.getInstance()
                        .getVertexCount(), Settings.getInstance().getFilling());
                GraphVisualization.getInstance().buildGraph(
                        GraphGenerator.generateGraph(Settings.getInstance()
                                .getVertexCount(), Settings.getInstance()
                                .getFilling()));

                GenerateWindow.this.hide();
            }

            private void setVertex() {
                String vertexS = vertexValue.getText();
                log.debug("Zapisywanie vertex value");
                Integer vertex = Integer.parseInt(vertexS);
                if (vertex != null) {
                    log.info("Odczytana wartosc vertex count: " + vertex);
                    settings.setVertexCount(vertex);
                }
            }

            private void setFillingValue() {
                String fillingS = fillingValue.getText();
                log.debug("Zapisywanie filling value");
                float filling = Float.parseFloat(fillingS);
                log.info("Odczytana wartosc filling: " + filling);
                settings.setFilling(filling);

            }
        });
    }

}
